home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-29 | 7.0 KB | 189 lines | [TEXT/CWIE] |
- //================================================================================
- // Greg Anderson
- // db+
- //
- // Transaction object
- // 17 May 1994
- // 31 Dec 1994
- //================================================================================
-
- #include "Transaction.h"
-
- #include "DBElement.h"
- #include "DBProperty.h"
- #include "DataRecord.h"
-
- //--------------------------------------------------------------------------------
- // TTransaction::~TTransaction
- //--------------------------------------------------------------------------------
- TTransaction::~TTransaction()
- {
- this->DiscardChanges();
- } // TTransaction::~TTransaction
-
- //--------------------------------------------------------------------------------
- // TTransaction::GetUpdatePointer
- //--------------------------------------------------------------------------------
- TTransactionAwareObject* TTransaction::GetUpdatePointer(AConst<TTransactionAwareObject> object)
- {
- Require(object.Exists());
- TTransactionAwareObject* newUP = nil;
-
- newUP = object->GrabUpdatePointer(this);
-
- REQUIREVALIDPOINTER(newUP);
-
- return newUP;
- } // TTransaction::GetUpdatePointer
-
- //--------------------------------------------------------------------------------
- // TTransaction::GetDBRecordUpdatePointer
- //--------------------------------------------------------------------------------
- AnUpdate<TDBRecord> TTransaction::GetDBRecordUpdatePointer(AConst<TTransactionAwareObject> cursor)
- {
- TTransactionAwareObject* newUP = this->GetUpdatePointer(cursor);
- return newUP->AbstractDBRecord();
- } // TTransaction::GetDBRecordUpdatePointer
-
- //--------------------------------------------------------------------------------
- // TTransaction::GetDBElementUpdatePointer
- //--------------------------------------------------------------------------------
- AnUpdate<TDBElement> TTransaction::GetDBElementUpdatePointer(AConst<TTransactionAwareObject> cursor)
- {
- TTransactionAwareObject* newUP = this->GetUpdatePointer(cursor);
- return newUP->DBElementRecord();
- } // TTransaction::GetDBElementUpdatePointer
-
- //--------------------------------------------------------------------------------
- // TTransaction::GetDBPropertyUpdatePointer
- //--------------------------------------------------------------------------------
- AnUpdate<TDBProperty> TTransaction::GetDBPropertyUpdatePointer(AConst<TTransactionAwareObject> cursor)
- {
- TTransactionAwareObject* newUP = this->GetUpdatePointer(cursor);
- return newUP->DBPropertyRecord();
- } // TTransaction::GetDBPropertyUpdatePointer
-
- //--------------------------------------------------------------------------------
- // TTransaction::GetDataRecordUpdatePointer
- //--------------------------------------------------------------------------------
- AnUpdate<TDataRecord> TTransaction::GetDataRecordUpdatePointer(AConst<TTransactionAwareObject> cursor)
- {
- TTransactionAwareObject* newUP = this->GetUpdatePointer(cursor);
- return newUP->DataRecord();
- } // TTransaction::GetDataRecordUpdatePointer
-
- //--------------------------------------------------------------------------------
- // TTransaction::GetDBElementUpdatePointer
- //--------------------------------------------------------------------------------
- AnUpdate<TDBRecord> TTransaction::GetDBRecordUpdatePointer(AConst<TDBRecord> cursor)
- {
- TTransactionAwareObject* newUP = this->GetUpdatePointer(cursor);
- return newUP->AbstractDBRecord();
- } // TTransaction::GetDBElementUpdatePointer
-
- //--------------------------------------------------------------------------------
- // TTransaction::GetDBElementUpdatePointer
- //--------------------------------------------------------------------------------
- AnUpdate<TDBElement> TTransaction::GetDBElementUpdatePointer(AConst<TDBElement> cursor)
- {
- TTransactionAwareObject* newUP = this->GetUpdatePointer(cursor);
- return newUP->DBElementRecord();
- } // TTransaction::GetDBElementUpdatePointer
-
- //--------------------------------------------------------------------------------
- // TTransaction::GetDBPropertyUpdatePointer
- //--------------------------------------------------------------------------------
- AnUpdate<TDBProperty> TTransaction::GetDBPropertyUpdatePointer(AConst<TDBProperty> cursor)
- {
- TTransactionAwareObject* newUP = this->GetUpdatePointer(cursor);
- return newUP->DBPropertyRecord();
- } // TTransaction::GetDBPropertyUpdatePointer
-
- //--------------------------------------------------------------------------------
- // TTransaction::GetDataRecordUpdatePointer
- //--------------------------------------------------------------------------------
- AnUpdate<TDataRecord> TTransaction::GetDataRecordUpdatePointer(AConst<TDataRecord> cursor)
- {
- TTransactionAwareObject* newUP = this->GetUpdatePointer(cursor);
- return newUP->DataRecord();
- } // TTransaction::GetDataRecordUpdatePointer
-
- //--------------------------------------------------------------------------------
- // TTransaction::CommitChanges
- //--------------------------------------------------------------------------------
- void TTransaction::CommitChanges()
- {
- //
- // Give every update pointer one last chance to do non-failsafe
- // operations before the transaction will actually commit.
- //
- for(TUpdatePointerCollectionIterator preIter(this); preIter.More(); preIter.Next())
- {
- preIter.Current()->UpdatePointer()->PrepareToCommit(this);
- }
-
- //
- // Once everyone is prepared, call CommitChanges. Throwing
- // an exception from CommitChanges is not allowed!
- //
- TUpdatePointerCollectionIterator iter(this);
- while(iter.More())
- {
- //
- // We don't provide a 'DeleteCurrent' method, because
- // the TUpdatePointerCollectionIterator is not allowed
- // to delete update pointers. Only the transaction
- // can do that.
- //
- TTransactionAwareObject* currentUP = iter.Current()->UpdatePointer();
- currentUP->CommitChanges(this);
- iter.Next();
-
- //
- // Tell the object that it no longer belongs to a transaction;
- //
- currentUP->SetTransaction(nil);
- }
-
- //
- // Once we have committed all of the changes and deleted
- // all of the update pointers, reset the transaction. It
- // may then be re-used for the next transaction or deleted.
- //
- this->ClearUpdatePointerCollection();
- } // TTransaction::CommitChanges
-
- //--------------------------------------------------------------------------------
- // TTransaction::DiscardChanges
- //--------------------------------------------------------------------------------
- void TTransaction::DiscardChanges()
- {
- TUpdatePointerCollectionIterator iter(this);
-
- while(iter.More())
- {
- //
- // We don't provide a 'DeleteCurrent' method, because
- // the TUpdatePointerCollectionIterator is not allowed
- // to delete update pointers. Only the transaction
- // can do that.
- //
- TTransactionAwareObject* currentUP = iter.Current()->UpdatePointer();
- currentUP->DiscardChanges(this);
- iter.Next();
-
- //
- // Tell the object that it no longer belongs to a transaction;
- //
- currentUP->SetTransaction(nil);
- }
-
- //
- // Once we have discarded all of the changes and deleted
- // all of the update pointers, reset the transaction. It
- // may then be re-used for the next transaction or deleted.
- //
- this->ClearUpdatePointerCollection();
- } // TTransaction::DiscardChanges
-
-